package af.fx.control.menu;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @Description
 * @Author AF
 * @Version 1.0.0
 * @Date 2024/1/30
 */
public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        // 菜单栏
        MenuBar menuBar = new MenuBar();
        // 一级菜单
        Menu menu1 = new Menu();
        menu1.setText("文件");
        menu1.setAccelerator(KeyCombination.valueOf("ctrl+i"));

        Menu menu2 = new Menu();
        menu2.setText("编辑");

        Menu menu3 = new Menu();
        menu3.setText("显示");

        // 子菜单分割符
        SeparatorMenuItem separ1 = new SeparatorMenuItem();
        SeparatorMenuItem separ2 = new SeparatorMenuItem();

        // 子菜单
        MenuItem menuItem1 = new MenuItem("打开");
        MenuItem menuItem2 = new MenuItem("新建");
        MenuItem menuItem3 = new MenuItem("复制");
        MenuItem menuItem4 = new MenuItem("剪切");
        MenuItem menuItem5 = new MenuItem("窗口工具");

        Menu m = new Menu();
        m.setText("拆分子菜单");
        MenuItem item6 = new MenuItem("AA");
        MenuItem item7 = new MenuItem("BB");
        m.getItems().addAll(item6, item7);

        menu1.getItems().addAll(menuItem1, separ1, menuItem2, separ2, m);
        menu2.getItems().addAll(menuItem3, menuItem4);
        menu3.getItems().addAll(menuItem5);

        // 单选按钮
        ToggleGroup tg = new ToggleGroup();
        RadioMenuItem rmi1 = new RadioMenuItem("单选");
        RadioMenuItem rmi2 = new RadioMenuItem("单选2");
        RadioMenuItem rmi3 = new RadioMenuItem("单选3");
        rmi1.setToggleGroup(tg);
        rmi2.setToggleGroup(tg);
        rmi3.setToggleGroup(tg);

        Menu menu = new Menu();
        menu.setText("单选菜单");
        menu.getItems().addAll(rmi1, rmi2, rmi3);
        rmi1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                RadioMenuItem item = (RadioMenuItem)event.getSource();
                System.out.println("是否被选择：" + item.isSelected());
            }
        });

        // 多选按钮
        CheckMenuItem c1 = new CheckMenuItem("多选1");
        CheckMenuItem c2 = new CheckMenuItem("多选2");
        CheckMenuItem c3 = new CheckMenuItem("多选3");
        Menu menu4 = new Menu();
        menu4.setText("多选菜单");
        menu4.getItems().addAll(c1, c2, c3);
        c1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                CheckMenuItem source = (CheckMenuItem) event.getSource();
                System.out.println(source.getText()+"被选择：" + source.isSelected());
            }
        });

        menuBar.getMenus().addAll(menu1, menu2, menu3, menu, menu4);

        // 只有下面有菜单的时候，才有效
        menu1.setOnShowing(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                System.out.println("menu1 setOnShowing");
            }
        });

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #ffff00");
        anchorPane.getChildren().add(menuBar);
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();

        menuBar.setPrefWidth(anchorPane.getWidth());
//        menuBar.setPrefHeight(50);

        anchorPane.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                menuBar.setPrefWidth(newValue.doubleValue());
            }
        });

        menuItem1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("点击了menuItem1");
            }
        });
    }
}
